package com.micro.frog.acct.rest;

import com.micro.frog.acct.dto.UserDetail;
import com.micro.frog.acct.utils.SubjectUtil;
import com.micro.frog.core.common.PageResult;
import com.micro.frog.core.common.Result;
import com.micro.frog.task.share.dto.TaskDTO;
import com.micro.frog.task.share.entity.Task;
import com.micro.frog.task.share.service.TaskControllerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 任务实现类
 *
 * @author :  ljh
 * @date : 2018/5/10
 **/
@Slf4j
@RestController
@RequestMapping(value = "/rbac/task")
public class TaskController {

    @Autowired
    TaskControllerService taskControllerService;

    @GetMapping(value = "/all")
    public Result all() {
        Result result = new Result();
        try {
            result = taskControllerService.all();
        } catch (Exception e) {
            result.setCode("20000");
            result.setMsg("系统异常" + e.getMessage());
            log.error("系统异常，e={}", e);
        }
        return result;
    }

    @GetMapping(value = "/list")
    public PageResult list(TaskDTO taskDTO) {
        PageResult result = new PageResult();
        try {
            result = taskControllerService.list(taskDTO);
        } catch (Exception e) {
            result.setCode("20000");
            result.setMsg("系统异常");
            log.error("系统异常，e={}", e);
        }
        return result;
    }

    @PostMapping("/add")
    public Result add(HttpServletRequest request, Task task) {
        Result result = new Result();
        try {
            UserDetail user = SubjectUtil.getUser(request);
            task.setCreatedBy(user.getUserName());
            result = taskControllerService.add(task);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("新增失败");
            log.error("新增失败 e={}", e);
        }
        return result;
    }

    @PostMapping("/delete")
    public Result deleteDict(Integer id) {
        Result result = new Result();
        try {
            result = taskControllerService.deleteDict(id);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    @PostMapping("/update")
    public Result update(HttpServletRequest request, Task task) {
        Result result = new Result();
        try {
            UserDetail user = SubjectUtil.getUser(request);
            task.setUpdatedBy(user.getUserName());
            result = taskControllerService.update(task);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg(e.getMessage());
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    @PostMapping("/run")
    public Result triggerSchedule(@RequestParam("taskIds[]") Integer[] taskIds, HttpServletRequest request) {
        Result result = new Result();
        try {
            UserDetail user = SubjectUtil.getUser(request);
            result = taskControllerService.triggerSchedule(taskIds, user.getUserName());
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }
}
